package com.my.study.structures.queue;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Carlos
 * @version 1.0
 * @Description 数组 -> 普通队列
 * @date 2021/6/2 22:48
 **/
@Slf4j(topic = "c.ArrayQueue")
public class ArrayQueue {

    /**
     * 最大容量
     */
    private int maxSize;

    /**
     * 队列头
     */
    private int front;

    /**
     * 队列尾
     */
    private int rear;

    private int[] arr;

    public ArrayQueue(){
        this(10);
    }

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = rear = -1;
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        if (isFull()) {
            log.debug("队列已满");
            return;
        }
        arr[++rear] = n;

    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取到数据");
        }

        int r = arr[++front];
        if (front == rear) {
            front = rear = -1;
        }
        return r;
    }

}
